package com.bdzl.ecs.util;


import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.common.utils.HttpUtil;


import com.bdzl.ecs.util.FddClient;
import com.fadada.sdk.util.crypt.FddEncryptTool;
import com.fadada.sdk.util.http.HttpsUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @创建人 wuwenjie
 * @创建时间 2019/1/7
 * @描述
 */
public class FddClientBaseUtils extends FddClient {
    public FddClientBaseUtils(String appId, String secret, String version, String url) {
        super(appId, secret, version, url);
    }


    /**
     *  修改个人信息
     * @param customer_id 客户id
     * @param email  邮箱
     * @param mobile  手机号码
     * @return
     */
    public String invokeInfoChangeAuto(String customer_id, String email, String mobile) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String msgDigest;
            // Base64(SHA1(appid+md5(timestamp)+SHA1(appsecret)))
            String sha1 = FddEncryptTool.sha1(super.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(super.getSecret()));
            msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));

            params.add(new BasicNameValuePair("customer_id", customer_id));
            params.add(new BasicNameValuePair("email", email));
            params.add(new BasicNameValuePair("mobile", mobile));

            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return HttpsUtil.doCaPost(super.getURLOfInfoChange(), params);
    }

    /**
     * 删除章接口
     * @param customer_id 客户id
     * @param signature_id 签章编号
     * @param
     * @return
     */
    public String invokeRemoveSignature(String customer_id,String signature_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {

            String timeStamp = HttpsUtil.getTimeStamp();
            String msgDigest;
            // Base64(SHA1(appid+md5(timestamp)+SHA1(appsecret)))
            String sha1 = FddEncryptTool.sha1(super.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(super.getSecret()+customer_id+signature_id));
            msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));

            params.add(new BasicNameValuePair("customer_id", customer_id));
            params.add(new BasicNameValuePair("signature_id", signature_id));

            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return HttpsUtil.doCaPost(super.getURLOfRemoveSignature(), params);
    }

    /**
     * 设置默认章接口
     * @param customer_id  客户id
     * @param signature_id  盖章编号
     * @param
     * @return
     */
    public String invokeDefaultSignature(String customer_id, String signature_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String msgDigest;
            // Base64(SHA1(appid+md5(timestamp)+SHA1(appsecret)))
            String sha1 = FddEncryptTool.sha1(super.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(super.getSecret()+customer_id+signature_id));
            msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));

            params.add(new BasicNameValuePair("customer_id", customer_id));
            params.add(new BasicNameValuePair("signature_id", signature_id));

            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        String json=JSONObject.toJSONString(params);
        System.out.println(json);
        System.out.println(super.getURLOfDefaultSignature());
        return HttpsUtil.doCaPost(super.getURLOfDefaultSignature(), params);
    }


    /**
     *  新建章接口
     * @param customer_id 客户id
     * @param signature_img_base64 印章图片
     * @return
     */
    public  String addSignature(String customer_id, String signature_img_base64) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String msgDigest;
            String sha1 = FddEncryptTool.sha1(super.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(super.getSecret()+customer_id+signature_img_base64));
            msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("customer_id", customer_id));
            params.add(new BasicNameValuePair("signature_img_base64",signature_img_base64));
            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return HttpsUtil.doCaPost(super.getURLOfAddSignature(), params);
    }

    /**
     *  查看印章图片
     * @param customer_id  客户id
     * @param signature_id  印章id
     * @return
     */
    public  String querySignature(String customer_id, String signature_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String msgDigest;
            String sha1 = FddEncryptTool.sha1(super.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(super.getSecret()+customer_id+signature_id));
            msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("customer_id", customer_id));
            params.add(new BasicNameValuePair("signature_id",signature_id));
            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
            System.out.println(params);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return HttpsUtil.doCaPost(super.getURLOfQuerySignature(), params);
    }


    /**
     *   替换印章图片
     * @param customer_id  客户id
     * @param signature_id  原印章图片id
     * @param signature_img_base64  新印章图片
     * @return
     */
    public  String replaceSignature(String customer_id,String signature_id ,String signature_img_base64) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String msgDigest;
            String sha1 = FddEncryptTool.sha1(super.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(super.getSecret()+customer_id+signature_id+signature_img_base64));
            msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("customer_id", customer_id));
            params.add(new BasicNameValuePair("signature_img_base64",signature_img_base64));
            params.add(new BasicNameValuePair("signature_id",signature_id));
            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return HttpsUtil.doCaPost(super.getURLOfReplaceSignature(), params);
    }
    /**
     *企业CA证书申请(免审核模式)
     * @param customerName 企业名称
     * @param email 电子邮箱 可选 可以为null
     * @param mobile  手机号码
     * @param organization   组织机构
     * @return
     */
    public  String syncCompanyAuto(String customerName,String email ,String mobile , String organization) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String msgDigest;
            String str = organization.concat("|").concat(mobile);
            String idMobile = FddEncryptTool.encrypt(str,super.getSecret());
            String sha1 = FddEncryptTool.sha1(super.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(super.getSecret()));
            msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("customer_name", customerName));
            params.add(new BasicNameValuePair("id_mobile",idMobile));
//            if (email!=null) {
//                params.add(new BasicNameValuePair("email", email));
//            }
            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return HttpsUtil.doCaPost(super.getURLOfSyncCompanyAuto(), params);
    }

    /**
     *客户编号查询接口
     * @param customerType 客户类型 1-个人 2-企业
     * @param identNo 证件号码
     * @param customerName 名称  需要 URLEncode 处理，编码 UTF-8
     * @return
     */
    public  String customerNumberQuery(String customerName,String customerType,String identNo) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            customerName = URLEncoder.encode(customerName,"utf-8");
            identNo = FddEncryptTool.encrypt(identNo,super.getSecret());
            String sha1 = FddEncryptTool.sha1(super.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(super.getSecret()+customerName+identNo));
            String msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("customer_name",customerName));
            params.add(new BasicNameValuePair("ident_no",identNo));
            params.add(new BasicNameValuePair("customer_type",customerType));
            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return HttpsUtil.doCaPost(super.getURLCustomerNumberQuery(), params);
    }

/*
    */
/**
     * 手动签署 指定签章
     * @param extSignEntity
     * @return
     *//*

    public  String invokeExtsignBySignature(ExtSignEntity extSignEntity) {
        StringBuilder sb = new StringBuilder(super.getURLOfExtSign());
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String sha1 = FddEncryptTool.sha1(super.getAppId() +
                    FddEncryptTool.md5Digest(extSignEntity.getTransactionId() + timeStamp ) +
                    FddEncryptTool.sha1(super.getSecret() + extSignEntity.getCustomerId() )
            );
            String msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            sb.append("?app_id=").append(super.getAppId());
            sb.append("&timestamp=").append(timeStamp);
            sb.append("&v=").append(super.getVersion());
            sb.append("&msg_digest=").append(msgDigest);
            sb.append("&customer_id=").append(extSignEntity.getCustomerId());
            sb.append("&return_url=").append(extSignEntity.getReturnUrl());
            sb.append("&sign_keyword=").append(extSignEntity.getSignKeyWord());
            sb.append("&doc_title=").append(extSignEntity.getDocTitle());
            sb.append("&signature_id=").append(extSignEntity.getSignatureId());
            sb.append("&contract_id=").append(extSignEntity.getContractId());
            sb.append("&transaction_id=").append(extSignEntity.getTransactionId());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return sb.toString() ;
    }
*/


    /**
     *客户签署结果查询接口
     * @param transactionId
     * @param contractId
     * @param customerId
     * @return
     */
    public  String querySignResult(String transactionId,String contractId,String customerId) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String sha1 = FddEncryptTool.sha1( super.getAppId() +
                    FddEncryptTool.md5Digest(timeStamp) +
                    FddEncryptTool.sha1(super.getSecret() + contractId+ customerId +transactionId )
            );
            String msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("contract_id", contractId));
            params.add(new BasicNameValuePair("customer_id", customerId));
            params.add(new BasicNameValuePair("transaction_id", transactionId));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return HttpsUtil.doCaPost(super.getURLOfQuerySignResult(), params);
    }



}
